package com.ymttest.business.service.shenqi;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.shop2cn.iapi.sqoptrade.parameter.enums.SqSellerRoleEnum;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sharding.model.ShenqiProxyChainPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;

public class CalculateShenqiSellerProfit {
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	public static BigDecimal calProductTotalProfit(List<OrderDetailPo> orderDetailList,
			Boolean isInherited, ShenqiProxyChainPo chainPo,
			List<ShenqiProxyChainPo> shenqiProxyChain, OrderPo orderPo,
			int index, BigDecimal taxFee, BigDecimal freight, Integer role, HashMap<String, BigDecimal> sellerProfitMap) {
		
		Long orderId = orderPo.getOrderId();
		
		// 店主的总收入
		BigDecimal shopOwnerTotalProfit = BigDecimal.ZERO;
		// 货头的总收入
		BigDecimal goodsOwnerTotalProfit = BigDecimal.ZERO;
		// 中间商的总收入
		BigDecimal middlerManTotalProfit = BigDecimal.ZERO;

		for (OrderDetailPo product : orderDetailList) {
			
			String catalogId = product.getCatalogId();
			
			// 分销小店没有优惠
			if (isInherited) {
				
				// chainPo取得是第一个商品的规格id，如果是多个规格，需要重新获取对应规格的chainPo
				if (!product.getCatalogId().equalsIgnoreCase(chainPo.getCatalogId())) {
							
					chainPo = orderShardingWapper
							.selectProxyChainbyOrderIdAndCatalogIdAndSellerId(
									orderId, catalogId, chainPo.getSellerId());
				}
				
				ShenqiProxyChainPo shopSellerChain = orderShardingWapper
						.selectProxyChainbyOrderIdAndCatalogIdAndSellerId(
								orderId, catalogId, orderPo.getSellerId());
				if (shopSellerChain.getFromParentPriceType() == 0) {

					BigDecimal shopOwnerProfit = (product
							.getProductPrice().subtract(shopSellerChain.getFromParentPrice()))
							.multiply(
									new BigDecimal(product
											.getProductCount()))
							.add(product.getDiscount());
					
					if (sellerProfitMap != null)
						sellerProfitMap.put(catalogId, shopOwnerTotalProfit);
					
					shopOwnerTotalProfit = shopOwnerTotalProfit.add(shopOwnerProfit);

				} else if (shopSellerChain.getFromParentPriceType() == 1) {

					BigDecimal shopOwnerProfit = (product
							.getProductPrice()
							.multiply(
									new BigDecimal(product
											.getProductCount()))
							.add(product.getDiscount()))
							.multiply(shopSellerChain.getFromParentPrice())
							.divide(new BigDecimal("100.00"));
					
					if (sellerProfitMap != null)
						sellerProfitMap.put(catalogId, shopOwnerTotalProfit);
					
					shopOwnerTotalProfit = shopOwnerTotalProfit.add(shopOwnerProfit);

				} else {

				}

				if (shenqiProxyChain.size() >= 3) {

					goodsOwnerTotalProfit = chainPo
							.getToChildPrice()
							.multiply(
									new BigDecimal(product
											.getProductCount()))
							.add(goodsOwnerTotalProfit);
					
					// 倒数第二个中间商利润按照商品实付价格
					if (index == shenqiProxyChain.size() - 1) {
						
						middlerManTotalProfit = middlerManTotalProfit
								.add(product
										.getProductPrice()
										.subtract(chainPo.getFromParentPrice())
										.multiply(
												new BigDecimal(product
														.getProductCount())));
						
						middlerManTotalProfit = middlerManTotalProfit.subtract(product.getIntegralDeductionAmount());
					} else
						middlerManTotalProfit = middlerManTotalProfit
								.add(chainPo
										.getToChildPrice()
										.subtract(chainPo.getFromParentPrice())
										.multiply(
												new BigDecimal(product
														.getProductCount())));
				}
			} else {
					
				ShenqiProxyChainPo chain = orderShardingWapper
						.selectProxyChainbyOrderIdAndCatalogIdAndSellerId(
								orderId, catalogId, chainPo.getSellerId());

				BigDecimal profit = product.getProductPrice().subtract(
						chain.getFromParentPrice());

				BigDecimal shopOwnerProfit = profit.multiply(
						new BigDecimal(product.getProductCount()));

				goodsOwnerTotalProfit = chain
						.getToChildPrice()
						.multiply(
								new BigDecimal(product
										.getProductCount()))
						.add(goodsOwnerTotalProfit);

				middlerManTotalProfit = middlerManTotalProfit
						.add(chain
								.getToChildPrice()
								.subtract(
										chain.getFromParentPrice())
								.multiply(
										new BigDecimal(product
												.getProductCount())));
				
				if (sellerProfitMap != null)
					sellerProfitMap.put(catalogId, shopOwnerProfit);
				
				shopOwnerTotalProfit = shopOwnerTotalProfit.add(shopOwnerProfit);
			}
		}
		
		shopOwnerTotalProfit = shopOwnerTotalProfit.subtract(orderPo.getIntegralDeductionAmount());
		
		if (SqSellerRoleEnum.GoodOwner.getCode() == role)
			return goodsOwnerTotalProfit;
		else if (SqSellerRoleEnum.ShopOwner.getCode() == role)
			return shopOwnerTotalProfit;
		else if (SqSellerRoleEnum.MiddlerMan.getCode() == role)
			return middlerManTotalProfit;
		else
			return BigDecimal.ZERO;
	}
	
	public static BigDecimal calProductProfit(OrderDetailPo product,
			Boolean isInherited, ShenqiProxyChainPo chainPo,
			List<ShenqiProxyChainPo> shenqiProxyChain, OrderPo orderPo,
			int index, Integer role) {
		
		// 店主的总收入
		BigDecimal shopOwnerTotalProfit = BigDecimal.ZERO;
		// 货头的总收入
		BigDecimal goodsOwnerTotalProfit = BigDecimal.ZERO;
		// 中间商的总收入
		BigDecimal middlerManTotalProfit = BigDecimal.ZERO;

		if (isInherited) {

			ShenqiProxyChainPo chain = shenqiProxyChain.get(shenqiProxyChain
					.size() - 1);
			if (chain.getFromParentPriceType() == 0) {

				shopOwnerTotalProfit = (product.getProductPrice()
						.multiply(new BigDecimal(product.getProductCount()))
						.add(product.getDiscount())
						.subtract(product.getPromotionAmount())
						.subtract(product.getSellerCouponAmount())).multiply(
						chain.getFromParentPrice());

			} else if (chain.getFromParentPriceType() == 1) {

				shopOwnerTotalProfit = (product.getProductPrice()
						.multiply(new BigDecimal(product.getProductCount()))
						.add(product.getDiscount())
						.subtract(product.getPromotionAmount())
						.subtract(product.getSellerCouponAmount()))
						.multiply(chain.getFromParentPrice())
						.divide(new BigDecimal("100.00"));

			} else {

			}

			if (shenqiProxyChain.size() >= 3) {

				goodsOwnerTotalProfit = chainPo.getToChildPrice()
						.multiply(new BigDecimal(product.getProductCount()));

				// 倒数第二个中间商利润按照商品实付价格
				if (index == shenqiProxyChain.size() - 1) {
					
					middlerManTotalProfit = product
							.getProductPrice()
							.subtract(chainPo.getFromParentPrice())
							.multiply(new BigDecimal(product.getProductCount()));
					
					middlerManTotalProfit = middlerManTotalProfit.subtract(product.getIntegralDeductionAmount());
				} else
					middlerManTotalProfit = chainPo
							.getToChildPrice()
							.subtract(chainPo.getFromParentPrice())
							.multiply(new BigDecimal(product.getProductCount()));
				
			} else 
				goodsOwnerTotalProfit = product.getProductPrice()
                        .multiply(new BigDecimal(product.getProductCount()))
						.subtract(shopOwnerTotalProfit);
		} else {

			BigDecimal profit = product.getProductPrice().subtract(
					chainPo.getFromParentPrice());

			shopOwnerTotalProfit = profit.multiply(
					new BigDecimal(product.getProductCount()));

			goodsOwnerTotalProfit = chainPo.getToChildPrice()
					.multiply(new BigDecimal(product.getProductCount()));

			middlerManTotalProfit = chainPo
					.getToChildPrice().subtract(chainPo.getFromParentPrice())
					.multiply(new BigDecimal(product.getProductCount()));
		}
		
		shopOwnerTotalProfit = shopOwnerTotalProfit.subtract(orderPo.getIntegralDeductionAmount());
		
		if (SqSellerRoleEnum.GoodOwner.getCode() == role)
			return goodsOwnerTotalProfit;
		else if (SqSellerRoleEnum.ShopOwner.getCode() == role)
			return shopOwnerTotalProfit;
		else if (SqSellerRoleEnum.MiddlerMan.getCode() == role)
			return middlerManTotalProfit;
		else
			return BigDecimal.ZERO;
	}
}
